import { login, logout, getMenuList, getUserInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import store from '@/store'
const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    userId: '',
    avatar: '',
    userInfo: {}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_userId: (state, userId) => {
    state.userId = userId
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  removeToken(state) {
    state.token = null // 将vuex的数据置空
    removeToken() // 同步到缓存
  },
  setUserInfo(state, value) {
    state.userId = value
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password })
        .then(response => {
          commit('SET_TOKEN', response.data)
          setToken(response.data)
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // get user MenuList
  getMenuList({ commit, state }) {
    return new Promise((resolve, reject) => {
      getMenuList(state.token)
        .then(response => {
          console.log('getMenuListresponse: ', response)
          const { data } = response
          if (!data) {
            return reject('Verification failed, please Login again.')
          }
          const { name, avatar } = data
          commit('SET_NAME', name)
          commit('SET_AVATAR', avatar)
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  // get user MenuList
  getUserInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getUserInfo(state.token)
        .then(response => {
          console.log('getUserInfo: ', response)
          commit('SET_userId', response.data.userId)
          commit('SET_NAME', response.data.userName)
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // user logout
  logout({ commit, state }) {
    // return new Promise((resolve, reject) => {
    // logout(state.token)
    // .then(() => {
    // removeToken() // must remove  token  first
    commit('removeToken')
    // 删除用户资料
    // context.commit('removeUseInfo')
    commit('RESET_STATE')
    resetRouter()
    commit('permission/setRoutes', [], { root: true })
    // store.state.hasPermission = false
    // commit('setHasPermission', false)
    // resolve()
    // })
    // .catch(error => {
    // reject(error)
    // })
    // })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
